package spotGUI.mainFrameGUI;

import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import managers.DBManager;
import managers.messengerManager.MessengerManager;
import managers.messengerManager.services.ChangeStateService;
import managers.messengerManager.services.Service;
import managers.socialNetworkManagers.ElementsManager;
import managers.socialNetworkManagers.SocialNetworkFeedbackManager;
import managers.socialNetworkManagers.SocialNetworkManager;
import spotGUI.utils.UserStateListCellRenderer;
import utils.MaxLenghtTextDocument;
import utils.StackTracePrinter;
import utils.UserState;
import elements.Element;

public class PersonalStateBar extends JPanel implements ItemListener {

	private JComboBox userState;
	private JTextField personalMessage;



	public PersonalStateBar() {



		setBorder(null);
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		personalMessage = new JTextField();
		personalMessage.setDocument(new MaxLenghtTextDocument(200));
		personalMessage.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					try {
						MessengerManager.getSharedInstance().getDataManager().updatePersonalMessage(personalMessage.getText());
						ElementsManager.addElement(DBManager.getConnection(), new Element(Element.PERSONAL_MESSAGE,
								SocialNetworkManager.getSharedInstance().getUserId(), ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("PersonalStateBar.2"), personalMessage.getText(), null));
						SocialNetworkManager.getSharedInstance().getUser(SocialNetworkManager.getSharedInstance().getUserId()).setPersonalMessage(personalMessage.getText());
					} catch (SQLException e1) {
						SocialNetworkManager.getSharedInstance().getFeedbackManager().updateStatus(
								ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("PersonalStateBar.1"), SocialNetworkFeedbackManager.WARNING);
						StackTracePrinter.print(e1);
					}
					SocialNetworkManager.getSharedInstance().getFeedbackManager().update();
				}
			}
		});

		personalMessage.setMaximumSize(new Dimension(2147483647, 35));
		personalMessage.setBackground(UIManager.getColor("Button.background"));
		personalMessage.setColumns(60);
		add(personalMessage);

		userState = new JComboBox();
		userState.setOpaque(false);
		userState.setBackground(UIManager.getColor("Button.background"));
		userState.setMinimumSize(new Dimension(190, 35));
		userState.setMaximumSize(new Dimension(190, 35));
		userState.setPreferredSize(new Dimension(190, 35));
		userState.setRenderer(new UserStateListCellRenderer());
		userState.setModel(new DefaultComboBoxModel(UserState.values()));
		userState.addItemListener(this);
		add(userState);

	}

	public void setState (UserState state) {
		userState.setSelectedItem(state);
	}

	public void setPersonalMessage (String message) {
		personalMessage.setText(message);
	}

	//programmatic state change, don't notify listeners!
	public void programmaticSetState (UserState state) {
		userState.removeItemListener(this);
		userState.setSelectedItem(state);
		userState.addItemListener(this);
	}


	@Override
	public void itemStateChanged(ItemEvent arg0) {

		if (arg0.getStateChange() == ItemEvent.SELECTED) {
			Service changeStateService = new ChangeStateService((UserState) arg0.getItem());
			MessengerManager.getSharedInstance().addService(changeStateService);
			changeStateService.start();
		}
	};

}